import kotlin.system.exitProcess

class Control {
    val board = Array(SIZE) { Array(SIZE) { 0 } }
    //转换选手
    fun switchPlayer(){
        when(currentPlayer){
            players[0] -> currentPlayer=players[1]
            players[1] -> currentPlayer=players[0]
        }
    }
    //落子
    fun drop(player: String){
        if (player == players[0]){
            board[currentRow][currentColumn]=1
        }else if (player == players[1]){
            board[currentRow][currentColumn]=-1
        }
    }
    //展示棋盘
    fun showChessbroad(){
        //列号
        print(" ")
        for (i in 1..SIZE)
            print(" $i")
        println()
        for (i in board.indices){
            print("${i+1} ")
            for (j in board[i].indices){
                if (board[i][j] == 0) {
                    print("+ ")
                }else if(board[i][j] == 1){
                    print("○ ")
                }else if(board[i][j] == -1){
                    print("● ")
                }
            }
            println()  // 换行
        }
    }
    //检查胜利
    fun checkWin(): Boolean {
        var count = 0
        //纵向判断
        for (i in -NumberOfConnected+1..<NumberOfConnected){
            //以防超出边界（代码复用？）
            val rowCurrent = currentRow - i
            if (rowCurrent<=0||rowCurrent>=SIZE){
                continue
            }
            //计数
            if (board[rowCurrent][currentColumn] != (if(currentPlayer==players[0]) 1 else -1)){
                count = -1
            }
            count++
            if (count == NumberOfConnected-1){
                return true
            }
        }
        count = 0
        //横向判断
        for (i in -NumberOfConnected+1..<NumberOfConnected){
            //以防超出边界
            val columnCurrent = currentColumn - i
            if (columnCurrent<=0||columnCurrent>=SIZE){
                continue
            }
            //计数
            if (board[currentRow][columnCurrent] != (if(currentPlayer==players[0]) 1 else -1)){
                count = -1
            }
            count++
            if (count == NumberOfConnected-1){
                return true
            }
        }
        count = 0
        //正对角判断
        for (i in -NumberOfConnected+1..<NumberOfConnected){
            //以防超出边界
            val rowCurrent = currentRow - i
            if (rowCurrent<=0||rowCurrent>=SIZE){
                continue
            }
            val columnCurrent = currentColumn - i
            if (columnCurrent<=0||columnCurrent>=SIZE){
                continue
            }
            //计数
            if (board[rowCurrent][columnCurrent] != (if(currentPlayer==players[0]) 1 else -1)){
                count = -1
            }
            count++
            if (count == NumberOfConnected-1){
                return true
            }
        }
        count = 0
        //反对角判断
        for (i in -NumberOfConnected+1..<NumberOfConnected){
            //以防超出边界
            val rowCurrent = currentRow - i
            if (rowCurrent<=0||rowCurrent>=SIZE){
                continue
            }
            val columnCurrent = currentColumn - i
            if (columnCurrent<=0||columnCurrent>=SIZE){
                continue
            }
            //计数
            if (board[rowCurrent][columnCurrent] != (if(currentPlayer==players[0]) 1 else -1)){
                count = -1
            }
            count++
            if (count == NumberOfConnected-1){
                return true
            }
        }
        return false
    }
    //获胜结束
    fun win(player: String){
        println("游戏结束！")
        println(player+"方胜利！")
        exitProcess(0)
    }
}